﻿using InCom.Entities.ProtocolEntity;
using InCom.SyncStatus;
using InCom.Utilities.Extension;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace inCOM实时数据补发功能
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {


        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            long oldcum = 0;
            InCom.OTM.Adapters.SyncStatusAdapter.Instance.StartSyncStatus();
            System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                while (true)
                {


                    ReashPoints();
                    long rsult = InCom.Core.TerminalSend.RealDataTransmissionSendBuffer.Instance.SendCount - oldcum;
                    oldcum = InCom.Core.TerminalSend.RealDataTransmissionSendBuffer.Instance.SendCount;
                    Console.WriteLine("发送速度:{0:F2}/s", rsult / 20.0);
                    System.Threading.Thread.Sleep(1000 * 20);
                }
            });
        }


        /// <summary>
        /// 刷新需要发送的目标节点
        /// </summary>
        public void ReashPoints()
        {
            foreach (var item in InCom.Core.SyncInteractive.SyncDataInteractiveSupervise.Instance.RealDataTransmissionDictionary.ToArray())
            {
                //添加或者更新所有节点
                foreach (var itemk in item.Value)
                {
                    points[itemk.Key] = itemk.Value;
                }

            }
        }
        /// <summary>
        /// 生成速度
        /// </summary>
        private int generateSpeed;



        /// <summary>
        /// 输出
        /// </summary>
        public bool CanRunCyclePlush循环添加模拟数据
        {
            get { return _canRunCyclePlush循环添加模拟数据; }
            set { _canRunCyclePlush循环添加模拟数据 = value; }
        }
        /// <summary>
        /// 生成速度
        /// </summary>
        public int GenerateSpeed
        {
            get
            {
                return generateSpeed;
            }

            set
            {
                generateSpeed = value;
            }
        }

        //多少秒发一次
        double ss = 1;


        /// <summary>
        /// 输出
        /// </summary>
        private bool _canRunCyclePlush循环添加模拟数据 = true;
        /// <summary>
        /// 需要发送的目标节点
        /// </summary>
        Dictionary<string, SyncPoint> points = new Dictionary<string, SyncPoint>();

        Action act = null;

        /// <summary>
        /// 开始模拟发送数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void but1_Click(object sender, RoutedEventArgs e)
        {

            if (act == null)
            {
                act = Cycle();
                Task.Factory.StartNew(act);
            }
        }

        /// <summary>
        /// 循环数据
        /// </summary>
        Action Cycle()
        {

            if (GenerateSpeed < 1)
            {
                GenerateSpeed = 10000;
            }
            int n = 0;
            int setp = 1;
            Action act1 = () =>
            {
                System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
                int num = 0;
                int s = 1;
                st.Restart();
                while (_canRunCyclePlush循环添加模拟数据)
                {
                    if (num < GenerateSpeed)
                    {
                        for (int k = 0; k < setp; k++)
                        {
                            num++;
                            var real = RealCreate();
                            InCom.Core.TerminalSend.RealDataTransmissionSendBuffer.Instance.Push(real, points);
                        }
                    }
                    else
                    {
                        st.Stop();
                        int nx = (int)st.ElapsedMilliseconds;
                        nx = (int)(ss * 1000) - nx;
                        if (nx > 0)
                        {
                            //做减法暂停
                            System.Threading.Thread.Sleep(nx);
                        }
                        else
                        {
                            //做减法暂停
                            System.Threading.Thread.Sleep(500);
                        }
                        num = 0;
                        st.Restart();
                        Dispatcher.BeginInvoke(new Action(() => { txt1.Text = string.Format("当前速度:{0:F2}/s,通道个数:{1}", GenerateSpeed / ss, dbc800.Length); }));

                    }
                }
            };

            return act1;
        }

        //800个DBC
        public decimal?[] dbc800 = System.Linq.Enumerable.Range(1, 800).Select(p => (decimal?)p).ToArray();
        string guid = Guid.NewGuid().ToString();

        Random dom = new Random();
        /// <summary>
        /// 实时数据创建
        /// </summary>
        /// <returns></returns>
        public RealtimeUpEntity RealCreate()
        {
            byte n = (byte)dom.Next(0, 100);
            RealtimeUpEntity incomProtocol = new RealtimeUpEntity()
            {
                Afn = 0x0C,
                #region 赋值
                AccelerometerX = n++,
                AccelerometerY = n++,
                AccelerometerZ = n++,
                AlarmBeginTime = DateTime.Now,
                AlarmType = n++,
                Altitude = n++,
                AutoId = n++,
                CanError = n++,
                ChargeDischarge = n++,
                ClientDbcmd5Value = guid,
                Data = dbc800,
                TravelTime = DateTime.Now,
                DbcVariableNameCount = (ushort)dbc800.Length,
                Destination = n++,
                DiagnosisResult = n++.ToString(),
                ErrorCode = n++.ToString(),
                GprsSignalStrength = n++,
                GpsCarSpeed = n++,
                GpsDirection = n++,
                GpsMileage = n++,
                IsCache = n++,
                IsCanHasData = n++,
                IsGpsPosition = n++,
                IsHasDynamic = true,
                IsHypervelocity = n++,
                IsLock = n++,
                IsMainPowerExists = n++,
                IsRealtimeCoordinate = n++,
                IsSlopOver = n++,
                IsTravel = n++,
                IsTriedDrive = n++,
                IsVehicleHasFault = n++,
                Latitude = n++,
                Longitude = n++,
                LockCarEquipmentState = n++,
                //PassthroughContext = item.PassthroughContext,
                //PassthroughMode = item.PassthroughMode,
                //PassthroughSeq = item.PassthroughSeq,
                //PassthroughVersion = item.PassthroughVersion,
                ResetNumber = n++,
                SdCardError = n++,
                ////////////////////////		=	item.	////////////////////////	,
                //IsMultiPacket = item.	//IsMultiPacket 	,
                //Length = item.	//Length 	,
                //MessageCurrentSEQ = item.	//MessageCurrentSEQ 	,
                //MessageTotalLength = item.	//MessageTotalLength 	,
                //MSGSEQ = item.	//MSGSEQ 	,
                //ProtocolUnitEntities = item.	//ProtocolUnitEntities 	,
                //ReceiveTime = item.	//ReceiveTime 	,
                //SendTime = item.	//SendTime 	,
                //SEQ = item.	//SEQ 	,
                //UserFlag = item.	//UserFlag 	,
                ///////////////////////////////////////		=	item.	///////////////////////////////////////	,
                SleepMode = n++,
                StateParameters = n++,
                // StatusData = item.StatusData,
                Switching = n++,
                TerminalCirTemp = n++,
                TerminalCode = n++,
                TerminalState = n++,
                Voltage1 = n++,
                Voltage2 = n++,


                TotalRunningTime = n++,
                #endregion
            };

            return incomProtocol;
        }

        /// <summary>
        /// 文字改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt2_TextChanged(object sender, TextChangedEventArgs e)
        {
            int.TryParse(txt2.Text, out generateSpeed);
        }


        #region 性能测试
        string hex = "7EAAEA00000000007A0000000000000000C05540000000000000564000000000004056400000000000405E4068000000670000006B0000006D000000690000005F0000006A00000070000000730000007B000000660000006E0000006C000000760000006F0000005E000000750000005D0000005C000000000000000102393762000000740000000000000000005F400000000000405F402433386637306434622D616233372D343638612D383934302D3464353631366161363164300000000000405C400000000000805C400000000000C056400000000000C0584000000000000059400000000000405940780000005A000000BC78A65BCA54D3087700000000000000BC78A65BCA54D30802003936200300000000000000200301000000000000F03F010000000000000040010000000000000840010000000000001040010000000000001440010000000000001840010000000000001C40010000000000002040010000000000002240010000000000002440010000000000002640010000000000002840010000000000002A40010000000000002C40010000000000002E40010000000000003040010000000000003140010000000000003240010000000000003340010000000000003440010000000000003540010000000000003640010000000000003740010000000000003840010000000000003940010000000000003A40010000000000003B40010000000000003C40010000000000003D40010000000000003E40010000000000003F40010000000000004040010000000000804040010000000000004140010000000000804140010000000000004240010000000000804240010000000000004340010000000000804340010000000000004440010000000000804440010000000000004540010000000000804540010000000000004640010000000000804640010000000000004740010000000000804740010000000000004840010000000000804840010000000000004940010000000000804940010000000000004A40010000000000804A40010000000000004B40010000000000804B40010000000000004C40010000000000804C40010000000000004D40010000000000804D40010000000000004E40010000000000804E40010000000000004F40010000000000804F40010000000000005040010000000000405040010000000000805040010000000000C05040010000000000005140010000000000405140010000000000805140010000000000C05140010000000000005240010000000000405240010000000000805240010000000000C05240010000000000005340010000000000405340010000000000805340010000000000C05340010000000000005440010000000000405440010000000000805440010000000000C05440010000000000005540010000000000405540010000000000805540010000000000C05540010000000000005640010000000000405640010000000000805640010000000000C05640010000000000005740010000000000405740010000000000805740010000000000C05740010000000000005840010000000000405840010000000000805840010000000000C05840010000000000005940010000000000405940010000000000805940010000000000C05940010000000000005A40010000000000405A40010000000000805A40010000000000C05A40010000000000005B40010000000000405B40010000000000805B40010000000000C05B40010000000000005C40010000000000405C40010000000000805C40010000000000C05C40010000000000005D40010000000000405D40010000000000805D40010000000000C05D40010000000000005E40010000000000405E40010000000000805E40010000000000C05E40010000000000005F40010000000000405F40010000000000805F40010000000000C05F40010000000000006040010000000000206040010000000000406040010000000000606040010000000000806040010000000000A06040010000000000C06040010000000000E06040010000000000006140010000000000206140010000000000406140010000000000606140010000000000806140010000000000A06140010000000000C06140010000000000E06140010000000000006240010000000000206240010000000000406240010000000000606240010000000000806240010000000000A06240010000000000C06240010000000000E06240010000000000006340010000000000206340010000000000406340010000000000606340010000000000806340010000000000A06340010000000000C06340010000000000E06340010000000000006440010000000000206440010000000000406440010000000000606440010000000000806440010000000000A06440010000000000C06440010000000000E06440010000000000006540010000000000206540010000000000406540010000000000606540010000000000806540010000000000A06540010000000000C06540010000000000E06540010000000000006640010000000000206640010000000000406640010000000000606640010000000000806640010000000000A06640010000000000C06640010000000000E06640010000000000006740010000000000206740010000000000406740010000000000606740010000000000806740010000000000A06740010000000000C06740010000000000E06740010000000000006840010000000000206840010000000000406840010000000000606840010000000000806840010000000000A06840010000000000C06840010000000000E06840010000000000006940010000000000206940010000000000406940010000000000606940010000000000806940010000000000A06940010000000000C06940010000000000E06940010000000000006A40010000000000206A40010000000000406A40010000000000606A40010000000000806A40010000000000A06A40010000000000C06A40010000000000E06A40010000000000006B40010000000000206B40010000000000406B40010000000000606B40010000000000806B40010000000000A06B40010000000000C06B40010000000000E06B40010000000000006C40010000000000206C40010000000000406C40010000000000606C40010000000000806C40010000000000A06C40010000000000C06C40010000000000E06C40010000000000006D40010000000000206D40010000000000406D40010000000000606D40010000000000806D40010000000000A06D40010000000000C06D40010000000000E06D40010000000000006E40010000000000206E40010000000000406E40010000000000606E40010000000000806E40010000000000A06E40010000000000C06E40010000000000E06E40010000000000006F40010000000000206F40010000000000406F40010000000000606F40010000000000806F40010000000000A06F40010000000000C06F40010000000000E06F40010000000000007040010000000000107040010000000000207040010000000000307040010000000000407040010000000000507040010000000000607040010000000000707040010000000000807040010000000000907040010000000000A07040010000000000B07040010000000000C07040010000000000D07040010000000000E07040010000000000F07040010000000000007140010000000000107140010000000000207140010000000000307140010000000000407140010000000000507140010000000000607140010000000000707140010000000000807140010000000000907140010000000000A07140010000000000B07140010000000000C07140010000000000D07140010000000000E07140010000000000F07140010000000000007240010000000000107240010000000000207240010000000000307240010000000000407240010000000000507240010000000000607240010000000000707240010000000000807240010000000000907240010000000000A07240010000000000B07240010000000000C07240010000000000D07240010000000000E07240010000000000F07240010000000000007340010000000000107340010000000000207340010000000000307340010000000000407340010000000000507340010000000000607340010000000000707340010000000000807340010000000000907340010000000000A07340010000000000B07340010000000000C07340010000000000D07340010000000000E07340010000000000F07340010000000000007440010000000000107440010000000000207440010000000000307440010000000000407440010000000000507440010000000000607440010000000000707440010000000000807440010000000000907440010000000000A07440010000000000B07440010000000000C07440010000000000D07440010000000000E07440010000000000F07440010000000000007540010000000000107540010000000000207540010000000000307540010000000000407540010000000000507540010000000000607540010000000000707540010000000000807540010000000000907540010000000000A07540010000000000B07540010000000000C07540010000000000D07540010000000000E07540010000000000F07540010000000000007640010000000000107640010000000000207640010000000000307640010000000000407640010000000000507640010000000000607640010000000000707640010000000000807640010000000000907640010000000000A07640010000000000B07640010000000000C07640010000000000D07640010000000000E07640010000000000F07640010000000000007740010000000000107740010000000000207740010000000000307740010000000000407740010000000000507740010000000000607740010000000000707740010000000000807740010000000000907740010000000000A07740010000000000B07740010000000000C07740010000000000D07740010000000000E07740010000000000F07740010000000000007840010000000000107840010000000000207840010000000000307840010000000000407840010000000000507840010000000000607840010000000000707840010000000000807840010000000000907840010000000000A07840010000000000B07840010000000000C07840010000000000D07840010000000000E07840010000000000F07840010000000000007940010000000000107940010000000000207940010000000000307940010000000000407940010000000000507940010000000000607940010000000000707940010000000000807940010000000000907940010000000000A07940010000000000B07940010000000000C07940010000000000D07940010000000000E07940010000000000F07940010000000000007A40010000000000107A40010000000000207A40010000000000307A40010000000000407A40010000000000507A40010000000000607A40010000000000707A40010000000000807A40010000000000907A40010000000000A07A40010000000000B07A40010000000000C07A40010000000000D07A40010000000000E07A40010000000000F07A40010000000000007B40010000000000107B40010000000000207B40010000000000307B40010000000000407B40010000000000507B40010000000000607B40010000000000707B40010000000000807B40010000000000907B40010000000000A07B40010000000000B07B40010000000000C07B40010000000000D07B40010000000000E07B40010000000000F07B40010000000000007C40010000000000107C40010000000000207C40010000000000307C40010000000000407C40010000000000507C40010000000000607C40010000000000707C40010000000000807C40010000000000907C40010000000000A07C40010000000000B07C40010000000000C07C40010000000000D07C40010000000000E07C40010000000000F07C40010000000000007D0140010000000000107D0140010000000000207D0140010000000000307D0140010000000000407D0140010000000000507D0140010000000000607D0140010000000000707D0140010000000000807D0140010000000000907D0140010000000000A07D0140010000000000B07D0140010000000000C07D0140010000000000D07D0140010000000000E07D0140010000000000F07D0140010000000000007D0240010000000000107D0240010000000000207D0240010000000000307D0240010000000000407D0240010000000000507D0240010000000000607D0240010000000000707D0240010000000000807D0240010000000000907D0240010000000000A07D0240010000000000B07D0240010000000000C07D0240010000000000D07D0240010000000000E07D0240010000000000F07D0240010000000000007F40010000000000107F40010000000000207F40010000000000307F40010000000000407F40010000000000507F40010000000000607F40010000000000707F40010000000000807F40010000000000907F40010000000000A07F40010000000000B07F40010000000000C07F40010000000000D07F40010000000000E07F40010000000000F07F40010000000000008040010000000000088040010000000000108040010000000000188040010000000000208040010000000000288040010000000000308040010000000000388040010000000000408040010000000000488040010000000000508040010000000000588040010000000000608040010000000000688040010000000000708040010000000000788040010000000000808040010000000000888040010000000000908040010000000000988040010000000000A08040010000000000A88040010000000000B08040010000000000B88040010000000000C08040010000000000C88040010000000000D08040010000000000D88040010000000000E08040010000000000E88040010000000000F08040010000000000F88040010000000000008140010000000000088140010000000000108140010000000000188140010000000000208140010000000000288140010000000000308140010000000000388140010000000000408140010000000000488140010000000000508140010000000000588140010000000000608140010000000000688140010000000000708140010000000000788140010000000000808140010000000000888140010000000000908140010000000000988140010000000000A08140010000000000A88140010000000000B08140010000000000B88140010000000000C08140010000000000C88140010000000000D08140010000000000D88140010000000000E08140010000000000E88140010000000000F08140010000000000F88140010000000000008240010000000000088240010000000000108240010000000000188240010000000000208240010000000000288240010000000000308240010000000000388240010000000000408240010000000000488240010000000000508240010000000000588240010000000000608240010000000000688240010000000000708240010000000000788240010000000000808240010000000000888240010000000000908240010000000000988240010000000000A08240010000000000A88240010000000000B08240010000000000B88240010000000000C08240010000000000C88240010000000000D08240010000000000D88240010000000000E08240010000000000E88240010000000000F08240010000000000F88240010000000000008340010000000000088340010000000000108340010000000000188340010000000000208340010000000000288340010000000000308340010000000000388340010000000000408340010000000000488340010000000000508340010000000000588340010000000000608340010000000000688340010000000000708340010000000000788340010000000000808340010000000000888340010000000000908340010000000000988340010000000000A08340010000000000A88340010000000000B08340010000000000B88340010000000000C08340010000000000C88340010000000000D08340010000000000D88340010000000000E08340010000000000E88340010000000000F08340010000000000F88340010000000000008440010000000000088440010000000000108440010000000000188440010000000000208440010000000000288440010000000000308440010000000000388440010000000000408440010000000000488440010000000000508440010000000000588440010000000000608440010000000000688440010000000000708440010000000000788440010000000000808440010000000000888440010000000000908440010000000000988440010000000000A08440010000000000A88440010000000000B08440010000000000B88440010000000000C08440010000000000C88440010000000000D08440010000000000D88440010000000000E08440010000000000E88440010000000000F08440010000000000F88440010000000000008540010000000000088540010000000000108540010000000000188540010000000000208540010000000000288540010000000000308540010000000000388540010000000000408540010000000000488540010000000000508540010000000000588540010000000000608540010000000000688540010000000000708540010000000000788540010000000000808540010000000000888540010000000000908540010000000000988540010000000000A08540010000000000A88540010000000000B08540010000000000B88540010000000000C08540010000000000C88540010000000000D08540010000000000D88540010000000000E08540010000000000E88540010000000000F08540010000000000F88540010000000000008640010000000000088640010000000000108640010000000000188640010000000000208640010000000000288640010000000000308640010000000000388640010000000000408640010000000000488640010000000000508640010000000000588640010000000000608640010000000000688640010000000000708640010000000000788640010000000000808640010000000000888640010000000000908640010000000000988640010000000000A08640010000000000A88640010000000000B08640010000000000B88640010000000000C08640010000000000C88640010000000000D08640010000000000D88640010000000000E08640010000000000E88640010000000000F08640010000000000F88640010000000000008740010000000000088740010000000000108740010000000000188740010000000000208740010000000000288740010000000000308740010000000000388740010000000000408740010000000000488740010000000000508740010000000000588740010000000000608740010000000000688740010000000000708740010000000000788740010000000000808740010000000000888740010000000000908740010000000000988740010000000000A08740010000000000A88740010000000000B08740010000000000B88740010000000000C08740010000000000C88740010000000000D08740010000000000D88740010000000000E08740010000000000E88740010000000000F08740010000000000F88740010000000000008840010000000000088840010000000000108840010000000000188840010000000000208840010000000000288840010000000000308840010000000000388840010000000000408840010000000000488840010000000000508840010000000000588840010000000000608840010000000000688840010000000000708840010000000000788840010000000000808840010000000000888840010000000000908840010000000000988840010000000000A08840010000000000A88840010000000000B08840010000000000B88840010000000000C08840010000000000C88840010000000000D08840010000000000D88840010000000000E08840010000000000E88840010000000000F08840010000000000F8884001000000000000894000000000000000000000000000000000000000000000000000000000000000000000007D02000000000000000000000000000000007E";
        InCom.API.Protocols.Parses.RealtimeUpEntityParse parse = new InCom.API.Protocols.Parses.RealtimeUpEntityParse();
        InCom.Logic.Process.Translation.TerminalRealtimeUpEntityTranslation trans = new InCom.Logic.Process.Translation.TerminalRealtimeUpEntityTranslation();


        #endregion
        /// <summary>
        /// 测试10万次解析
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Click(object sender, RoutedEventArgs e)
        {

            var api = trans.GetInComAPIEntity(RealCreate());
            byte[] parsearr = parse.Create(api);
            System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();

            st.Restart();
            //10万次解析
            for (int i = 0; i < 10 * 10000; i++)
            {
                var dd = parse.Decompose(parsearr);

            }
            st.Stop();

            Console.WriteLine("10万次800DBC解析:{0:F2}/s", 10 * 10000 / (st.ElapsedMilliseconds / 1000.0));

        }
    }
}
